﻿using System;
using System.Collections.Generic;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.Graphics;
using FlatRedBall.Gui;
using FlatRedBall.Math.Geometry;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using ROMAP.Utilities;
using ROMAP.Entities.Menu;

namespace ROMAP.Screens
{
    public class LevelScreen : Screen
    {
        private Sprite background;
        private MenuButton mainMenuButton;
        private MenuButton level1Button;
        private MenuButton level2Button;
        private MenuButton level3Button;
        private MenuButton level4Button;
        private MenuButton level5Button;
        private MenuButton level6Button;
        private MenuButton level7Button;
        private MenuButton level8Button;
        #region Methods

        #region Constructor and Initialize

        public LevelScreen()
            : base("LevelScreen")
        {
            // Don't put initialization code here, do it in
            // the Initialize method below
            //   |   |   |   |   |   |   |   |   |   |   |
            //   |   |   |   |   |   |   |   |   |   |   |
            //   V   V   V   V   V   V   V   V   V   V   V
            //Initialize(true);
        }

        public override void Initialize(bool addToManagers)
        {
            if (addToManagers)
            {
                AddToManagers();
            }
        }

        public override void AddToManagers()
        {
            // Set the screen up here instead of in the Constructor to avoid
            // exceptions occurring during the constructor.
            FlatRedBallServices.IsWindowsCursorVisible = true;

            // Initialize the sprites
            // Initialize background sprite
            background = SpriteManager.AddSprite(@"Content/Menu/MenuBackground");
            background.PixelScale();
            this.mSprites.Add(background);

            GameManager.DeathCounter = 0;

            mainMenuButton = new MenuButton("Global", @"Content/Menu/MainMenuButton", "", 'e', 0, -12);
            mainMenuButton.collision.Visible = false;
            level1Button = new MenuButton("Global", @"Content/Menu/Levels/1", "", 'n', -18, 3);
            level1Button.MultScaleX(0.5f);
            level1Button.MultScaleY(0.5f);
            level2Button = new MenuButton("Global", @"Content/Menu/Levels/2", "", 'n', -14, 3);
            level2Button.MultScaleX(0.5f);
            level2Button.MultScaleY(0.5f);
            level3Button = new MenuButton("Global", @"Content/Menu/Levels/3", "", 'n', -10, 3);
            level3Button.MultScaleX(0.5f);
            level3Button.MultScaleY(0.5f);
            level4Button = new MenuButton("Global", @"Content/Menu/Levels/4", "", 'n', -6, 3);
            level4Button.MultScaleX(0.5f);
            level4Button.MultScaleY(0.5f);
            level5Button = new MenuButton("Global", @"Content/Menu/Levels/5", "", 'n', -2, 3);
            level5Button.MultScaleX(0.5f);
            level5Button.MultScaleY(0.5f);
            level6Button = new MenuButton("Global", @"Content/Menu/Levels/6", "", 'n', 2, 3);
            level6Button.MultScaleX(0.5f);
            level6Button.MultScaleY(0.5f);
            level7Button = new MenuButton("Global", @"Content/Menu/Levels/7", "", 'n', 6, 3);
            level7Button.MultScaleX(0.5f);
            level7Button.MultScaleY(0.5f);
            level8Button = new MenuButton("Global", @"Content/Menu/Levels/8", "", 'n', 10, 3);
            level8Button.MultScaleX(0.5f);
            level8Button.MultScaleY(0.5f);
        }
        #endregion

        #region Public Methods

        public override void Activity(bool firstTimeCalled)
        {
            checkCollisions();
        }

        public override void Destroy()
        {
            base.Destroy();
            mainMenuButton.Destroy();
            level1Button.Destroy();
            level2Button.Destroy();
            level3Button.Destroy();
            level4Button.Destroy();
            level5Button.Destroy();
            level6Button.Destroy();
            level7Button.Destroy();
            level8Button.Destroy();
            SpriteManager.RemoveSprite(background);

        }

        private void checkCollisions()
        {
            // Mouse collision check
            // Is Mouse on any button?
            if (InputManager.Mouse.IsOn(mainMenuButton.collision))
            {
                mainMenuButton.Hover();
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    MoveToScreen(typeof(MenuScreen).FullName);
                }
            }
            else
                mainMenuButton.Idle();
            if (InputManager.Mouse.IsOn(level1Button.collision))
            {
                level1Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 1;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level1Button.Idle();

            if (InputManager.Mouse.IsOn(level2Button.collision))
            {
                level2Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 2;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level2Button.Idle();

            if (InputManager.Mouse.IsOn(level3Button.collision))
            {
                level3Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 3;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level3Button.Idle();

            if (InputManager.Mouse.IsOn(level4Button.collision))
            {
                level4Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 4;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level4Button.Idle();

            if (InputManager.Mouse.IsOn(level5Button.collision))
            {
                level5Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 5;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level5Button.Idle();

            if (InputManager.Mouse.IsOn(level6Button.collision))
            {
                level6Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 6;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level6Button.Idle();

            if (InputManager.Mouse.IsOn(level7Button.collision))
            {
                level7Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 7;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level7Button.Idle();

            if (InputManager.Mouse.IsOn(level8Button.collision))
            {
                level8Button.Hover();
                // Is left button pressed?
                if (InputManager.Mouse.ButtonPushed(FlatRedBall.Input.Mouse.MouseButtons.LeftButton))
                {
                    Destroy();
                    GameManager.LevelNum = 8;
                    MoveToScreen(typeof(GameScreen).FullName);
                }
            }
            else
                level8Button.Idle();
        }
        #endregion

        #endregion
    }
}
